<?php

namespace System\Controllers;

class Login extends \System\Core\Controller
{
    private $data = array();
    
    public function __construct()
    {
        parent::__construct();
                
        $this->load->helper('form');
        $this->load->library('validation');
    }
    
    public function indexAction()
    {
        $this->view->templateRender('outside', 'login/form', 'Log in', $this->data);
    }
    
    public function submitAction()
    {
        if ($this->input->isPost())
        {
            // Do some sanity testing on the values.
            $this->validation->addRule('userName', 'user name', 'trim|req');
            $this->validation->addRule('password', 'password', 'trim|req');
            if ($this->validation->run())
            {
                // Try and log in the user
                if ($this->curuser->logIn($this->input->post('userName'), $this->input->post('password')))
                {
                    $this->fc->siteRedirect('index');
                }
                else
                {
                    $this->data['errors'][] = 'User name or password is incorrect.';
                }
            }
            else
            {
                $this->data['errors'] = $this->validation->getErrors();
            }
            
            $this->indexAction();
        }
        else
        {
            $this->fc->siteRedirect('login');
        }
    }
}

?>
